%{
#include "YaccParser.h"

extern char yytext[];

int yylex(void);
void yyerror(char *s);
int yyparse(void);
%}


%union {
	int iValue;
	SymbolBase * pSym;
	void * pAST;
	char * strName;
};

%token INCLUDE_START INCLUDE_END

%token BOUNDER TOKEN UNION TYPE NONASSOC START 

%token IDENTIFIER DEFINETOKEN CONVENTIONTOKEN CHARACTER

%type <pAST> token_list item_list item atom
%type <pSym> IDENTIFIER DEFINETOKEN CONVENTIONTOKEN 
%%

token_define:
	TOKEN token_list			{ Conventions::getInstance().addDefinedToken((IdentifierList *)$2); }
	;

token_list:
	IDENTIFIER					{ Identifier * pId = new Identifier((SymbolBase *)$1); $$ = new IdentifierList(pId); }
	| token_list IDENTIFIER		{ IdentifierList *pList = (IdentifierList *)$1; pList->addIdentifier(new Identifier((SymbolBase *)$2)); $$ = pList;}
	;

start_item:
	START IDENTIFIER			
	;

type_define:
	TYPE '<' IDENTIFIER '>' token_list
	;

item_convention:
	IDENTIFIER ':' item_list ';'		{ConventionItem * pConvention = new ConventionItem((SymbolBase *)$1, (ItemList *)$3); Conventions::getInstance().addConvention(pConvention);}

item_list:
	item								{std::vector<Item *> *pList = new std::vector<Item *>(); pList->push_back((Item *)$1); $$ = pList;}
	| item_list '|' item					{((std::vector<Item *> *)$1)->push_back((Item *)$3); $$ = $1;}
	;

item:
	atom								{ Item *pItem = new Item((Atom *)$1); $$ = pItem;}
	| item atom							{ Item *pItem = (Item *)$1; pItem->addAtom((Atom *)$2); $$ = pItem;}
	;

atom:
	DEFINETOKEN							{ $$ = new Atom((SymbolBase *)$1); }
	| IDENTIFIER						{ $$ = new Atom((SymbolBase *)$1); }
	| CONVENTIONTOKEN					{ $$ = new Atom((SymbolBase *)$1); }
	| CHARACTER							{ $$ = new Atom(yytext[1]); }
	;


%%
#include <stdio.h>




extern FILE * yyin;
extern int column;
extern int row;


void yyerror(char *s)
{

  fflush(stdout);

  printf("Error happen : %s\n", yytext);

}
int main(int argc, char *argv[])
{
	yyin = fopen(argv[1],"r");
	yyparse();
	return 1;
}





